﻿using Gamelight.Scene;
using Gamelight.Graphics;
using Gamelight.Input;

namespace NpPuzzleGame
{
	public class PlayScene : GameSceneBase
	{
		private static readonly Color blockColor = Colors.DarkGray;
		private static readonly Color playerColor = Colors.Blue;

		private Sprite player = new Sprite(120, 104, 16, 16);

		private Level level = new Level();

		protected override void Initialize()
		{
		}

		protected override void ProcessInput(InputEvent[] events)
		{
			int velocity = 2;
			int dx = 0;
			int dy = 0;
			if (InputManager.IsKeyPressed(Key.Left)) dx = -1;
			if (InputManager.IsKeyPressed(Key.Right)) dx = 1;
			if (InputManager.IsKeyPressed(Key.Up)) dy = -1;
			if (InputManager.IsKeyPressed(Key.Down)) dy = 1;

			this.player.DX = dx * velocity;
			this.player.DY = dy * velocity;
		}

		protected override void Render(Image gameScreen)
		{
			gameScreen.Fill(Colors.Black);
			Draw.Rectangle(gameScreen, PlayScene.playerColor, this.player.X, this.player.Y, this.player.Width, this.player.Height);

			foreach (Block block in this.level.AllBlocks)
			{
				Draw.Rectangle(gameScreen, PlayScene.blockColor, block.Left, block.Top, block.Width, block.Height);
			}
		}

		protected override void Update(int gameCounter)
		{
			this.level.TryUpdatePosition(this.player);
		}
	}
}